using System;
using System.Collections.Generic;
using Magiq.Support;
using Magiq.Tests.Support;
using NUnit.Framework;

namespace Magiq.Tests
{
    [TestFixture]
    public class WeakDictionaryTests
    {
        #region Setup/Teardown

        [SetUp]
        public void SetUp()
        {
            dictionary = new WeakDictionary<int, string>();
        }

        #endregion

        private WeakDictionary<int, string> dictionary;

        [Test]
        public void ShouldAdd()
        {
            dictionary.Add(1, "lala");
            dictionary.Add(2, "lele");
            dictionary[1].Should(Be.EqualTo("lala"));
            dictionary[2].Should(Be.EqualTo("lele"));
        }

        [Test]
        public void ShouldCount()
        {
            dictionary.Add(1, "lala");
            dictionary.Add(2, "lele");
            dictionary.Count.Should(Be.EqualTo(2));
        }

        [Test]
        public void ShouldRemove()
        {
            dictionary.Add(1, "lala");
            dictionary.Add(2, "lele");
            dictionary.Remove(1);
            dictionary.Count.Should(Be.EqualTo(1));
        }

        [Test]
        public void ShouldTellIfContainsAnItem()
        {
            dictionary.Add(1, "lala");
            dictionary.Add(2, "lele");
            dictionary.ContainsKey(1).Should(Be.True);
        }

        [Test]
        public void ShouldTellIfDoesntContainAnItem()
        {
            dictionary.Add(1, "lala");
            dictionary.Add(2, "lele");
            dictionary.ContainsKey(3).Should(Be.False);
        }

        [Test]
        [ExpectedException(typeof (ArgumentException))]
        public void ShouldThrowIfAnItemIsAddedTwice()
        {
            dictionary.Add(1, "lala");
            dictionary.Add(1, "lele");
        }

        [Test]
        [ExpectedException(typeof (KeyNotFoundException))]
        public void ShouldThrowWhenGettingFromIndexAndTheKeyDoesntExist()
        {
            dictionary.Add(1, "lala");
            dictionary.Add(2, "lele");
            var item = dictionary[3];
        }

        [Test]
        public void ShouldThrowWhenUpdatingAnItemThatDoesntExist()
        {
            dictionary.Add(1, "lala");
            dictionary.Add(2, "lele");
            dictionary[3] = "lolo";
        }

        [Test]
        public void ShouldUpdateAddedItem()
        {
            dictionary.Add(1, "lala");
            dictionary.Add(2, "lele");
            dictionary[1] = "lolo";
            dictionary[1].Should(Be.EqualTo("lolo"));
            dictionary[2].Should(Be.EqualTo("lele"));
        }
    }
}